package com.topscomm.base.job;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.topscomm.base.service.IBaseRemindTaskService;
import com.topscomm.cbo.pojo.CboSysParamEntity;
import com.topscomm.cbo.service.ICboSysParamService;
import com.topscomm.pub.util.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;

@JobHandler(value = "ltcRemindTaskToMessageJob")
@Component
public class RemindTaskToMessageJob extends IJobHandler {
	@Autowired
	IBaseRemindTaskService baseRemindTaskService;
	@Autowired
	private ICboSysParamService cboSysParamService;

	/**
	 * @description: 按照RemindTask表的记录定时发送消息
	 * @param param
	 * @return
	 * @throws Exception
	 * @author: maxiaodong
	 * @date: 2020年12月10日下午2:04:48
	 * @modify:
	 */
	@Override
	public ReturnT<String> execute(String param) throws Exception {
		Date currentTime = new Date();
		String currentTimeString = DateUtil.dateToString(currentTime, "yyyy-MM-dd HH:mm:ss");
		// 获取夜间停止提醒开始时间
		CboSysParamEntity beginTimecboSysParamEntity = this.cboSysParamService.queryObjectByWhere("code='BeginTimeStopRemindTask'");
		String beginTimeStopRemindTaskString = beginTimecboSysParamEntity.getParamvalue();
		beginTimeStopRemindTaskString = currentTimeString.split(" ")[0] + " " + beginTimeStopRemindTaskString;
		Date beginTimeStopRemindTask = DateUtil.stringToDate(beginTimeStopRemindTaskString);
		// 获取夜间停止结束时间
		CboSysParamEntity endTimecboSysParamEntity = this.cboSysParamService.queryObjectByWhere("code='EndTimeStopRemindTask'");
		String endTimeStopRemindTaskString = endTimecboSysParamEntity.getParamvalue();
		endTimeStopRemindTaskString = currentTimeString.split(" ")[0] + " " + endTimeStopRemindTaskString;
		Date endTimeStopRemindTask = DateUtil.stringToDate(endTimeStopRemindTaskString);
		if (beginTimeStopRemindTask != null && endTimeStopRemindTask != null) {
			// 配置正确进行时间检测
			if (currentTime.getTime() >= endTimeStopRemindTask.getTime() && currentTime.getTime() <= beginTimeStopRemindTask.getTime()) {
				baseRemindTaskService.taskToMessage();
			}
		} else {
			// 若配置不正确，不进行时间检测
			baseRemindTaskService.taskToMessage();
		}
		return ReturnT.SUCCESS;
	}
}
